import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { UserEntity } from './user.entity'

@Injectable()
export class UserService {
    constructor(@InjectRepository(UserEntity) private readonly userRepository: Repository<UserEntity>) {}

    async findAll(): Promise<UserEntity[]> {
        return await this.userRepository.find({
            select: ['username'],
            skip: 0,
            take: 2
        })
    }

    /**
     *
     * @param id {number} 对应数据库中的表id，默认是1，根据需要传入数值
     * @returns {object} 返回查询结果 {"id":2,"username":"bbbbbb","password":"b22222222"}
     * @调用方法 [service].findOneBy(2)
     */
    findOne(id?: number): Promise<UserEntity | null> {
        return this.userRepository.findOneBy({ id })
        // return this.userRepository.findOne({
        //     where: { id }
        // })
    }

    async del(id: number): Promise<UserEntity | void> {
        await this.userRepository.delete(id)
    }
}
